<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_计算器练习</title>
</head>
<body>
    <input type="text" id="i1" placeholder="请输入数字1">
    <input type="text" id="i2" placeholder="请输入数字2">
    <br>
    <button onclick="calc('+')" id="b1">加</button>
    <button onclick="calc('-')" id="b2">减</button>
    <button onclick="calc('*')" id="b3">乘</button>
    <button onclick="calc('/')" id="b4">除</button>
    <h1>运算结果：<span></span></h1>
<script>
    //1.定义函数
    function calc(o) {
        //2.获取用户在输入框中输入的数据
        let n1 = document.querySelector('#i1').value;
        let n2 = document.querySelector('#i2').value;
        //3.将获取到的数据转换为number类型
        n1 = parseFloat(n1);
        n2 = parseFloat(n2);
        //4.获取用来显示结果的span元素
        let spanE = document.querySelector('span');
        //5.根据用户输入的运算符，计算结果
        switch (o) {
            case '+':
                spanE.innerHTML = n1 + n2;
                break;
            case '-':
                spanE.innerHTML = n1 - n2;
                break;
            case '*':
                spanE.innerHTML = n1 * n2;
                break;
            case '/':
                if (n2 != 0) {
                    spanE.innerHTML = n1 / n2;
                } else {
                    alert('除数不能为0！')
                }
        }
    }
</script>
</body>
</html>